﻿using System;
using System.Data.OleDb;
using System.Windows;
using AEM.Common;
using AEM.Data;
using AEM.Data.BLL.Users;
using Newtonsoft.Json;
using Actions = AEM.Data.BLL.Actions;

namespace AEM
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login : Window
    {
        public Login()
        {
            InitializeComponent();
            this.MaxHeight = System.Windows.SystemParameters.WorkArea.Height;
            this.MaxWidth = System.Windows.SystemParameters.WorkArea.Width;
            this.WindowState = WindowState.Maximized;
            this.btn_Login.Click += new RoutedEventHandler(btn_Login_Click);
            this.btn_modify.Click += new RoutedEventHandler(btn_modify_Click);
            this.btn_cancel.Click += new RoutedEventHandler(btn_cancel_Click);
            this.btn_cancel1.Click += new RoutedEventHandler(btn_cancel_Click);
            this.Loaded += new RoutedEventHandler(Login_Loaded);
        }

        void Login_Loaded(object sender, RoutedEventArgs e)
        {
            this.lbVersion.Content = "AEM Copyright(C) 2023 v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            string time = DateTime.Now.ToString("yyyy-MM-dd");
            lbTimer.Content = time;

            double top = this.Height * 0.5;
            this.loginTitle.Margin = new Thickness(0, this.Height * 0.42, 0, 0);
            this.dockPanelLogin.Margin = new Thickness(0, top, 0, 0);
            this.dockPanelChangePassword.Margin = new Thickness(0, top, 0, 0);


#if DEBUG
            txt_username.Text = "admin";
            txt_password.Password = "";
#endif
        }
        void btn_Login_Click(object sender, RoutedEventArgs e)
        {
            msg_login.Text = "";
            if (string.IsNullOrEmpty(txt_username.Text.Trim()))
            {
                msg_login.Text = "请输入用户名！";
                txt_username.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txt_password.Password.Trim()))
            {
                msg_login.Text = "请输入密码！";
                txt_password.Focus();
                return;
            }
            kk();
            Actions.User.UserLoginResult result = Actions.User.UserLogin(txt_username.Text.Trim(), txt_password.Password.Trim());
            switch (result)
            {
                case Actions.User.UserLoginResult.UserNameIsNotExist:
                    msg_login.Text = "用户名不存在！";
                    txt_username.Focus();
                    break;
                case Actions.User.UserLoginResult.UserPWDIsWrong:
                    msg_login.Text = "密码不正确，请核对后重试！";
                    txt_password.Focus();
                    break;
                case Actions.User.UserLoginResult.OK:
                    if (Actions.User.IsFirstLogin())
                    {
                        this.dockPanelLogin.Visibility = Visibility.Collapsed;
                        this.dockPanelChangePassword.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        LoginSuccessInit loginInit = new LoginSuccessInit(Config.userLogin);
                        loginInit.InitSuccessEvent += new EventHandler(loginInit_InitSuccessEvent);
                        //LoadMainWindow();
                    }
                    break;
                default:
                    msg_login.Text = result.ToString();
                    break;
            }
        }

        public void kk()
        {
            var providers = OleDbEnumerator.GetRootEnumerator().GetEnumerator();
            while (providers.MoveNext())
            {
                //LogHelper.Log(JsonConvert.SerializeObject(providers.Current));
            }
        }

        void btn_modify_Click(object sender, RoutedEventArgs e)
        {
            msg_modify.Text = "";
            if (string.IsNullOrEmpty(txt_oldpwd.Password.Trim())) { msg_modify.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("PasswordOld") as string); txt_oldpwd.Focus(); return; }
            if (string.IsNullOrEmpty(txt_newpwd.Password.Trim())) { msg_modify.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("PasswordNew") as string); txt_newpwd.Focus(); return; }
            if (string.IsNullOrEmpty(txt_newpwd_re.Password.Trim())) { msg_modify.Text = string.Format(this.TryFindResource("NotBeNullOrEmpty") as string, this.TryFindResource("PasswordReply") as string); txt_newpwd_re.Focus(); return; }
            if (txt_newpwd.Password.Trim() != txt_newpwd_re.Password.Trim()) { msg_modify.Text = this.TryFindResource("ComparePassword") as string; txt_newpwd_re.Focus(); return; }

            Actions.User.ModifiyUserResult result = Actions.User.ModifiyPassword(txt_oldpwd.Password.Trim(), txt_newpwd.Password.Trim());
            switch (result)
            {
                case Actions.User.ModifiyUserResult.UserIsNotExist:
                    msg_modify.Text = string.Format(this.TryFindResource("IsNotExist") as string, this.TryFindResource("PasswordReset") as string);
                    break;
                case Actions.User.ModifiyUserResult.WrongPassword:
                    msg_modify.Text = this.TryFindResource("CompareOldPassword") as string;
                    txt_oldpwd.Focus();
                    break;
                case Actions.User.ModifiyUserResult.OK:
                    Actions.Log.AddOperation.Invoke("=InfoChangePassword");
                    LoginSuccessInit loginInit = new LoginSuccessInit(Config.userLogin);
                    //loginInit.InitSuccessEvent += new EventHandler(loginInit_InitSuccessEvent);
                    LoadMainWindow();
                    break;
                default:
                    msg_modify.Text = result.ToString();
                    break;
            }
        }
        bool isopen = false;
        private void LoadMainWindow()
        {
            if (isopen) return;
            isopen = true;
            Actions.Log.AddOperation.Invoke("=InfoLogin");

            Dispatcher.Invoke(new Action(() =>
            {
                Splasher.ShowSplash();
#if !DEBUG
                Splasher.Splash.Topmost = true;
#endif
                MessageListener.Instance.ReceiveMessage(this.TryFindResource("LoadingInfo01") as string);
                this.Hide();
                (new MainWindow()).Show();
                this.Close();
            }));
        }

        private void loginInit_InitSuccessEvent(object sender, EventArgs e)
        {
            LoadMainWindow();
        }

        void btn_cancel_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }
    }
}
